﻿/******************************************************************************************************
Copyright (c) 2014 Steven Giacomelli (stevegiacomelli@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************************************/

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace SharpGeo
{
    /// <summary>
    /// An lightweight immutable class to store a coordinate projected to a screen in Cartesian Space (X,Y)
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    [DebuggerDisplay("({X},{Y})")]
    sealed public class ScreenCoordinate :
        IEquatable<IScreenCoordinate>,
        IScreenCoordinate, ICoordinate
    {
        #region Constructors

        /// <summary>
        /// Constructs a ScreenCoordinate
        /// </summary>
        /// <param name="x">The X value</param>
        /// <param name="y">The Y value</param>
        public ScreenCoordinate(double x, double y)
        {
            _x = x;
            _y = y;
        }

        /// <summary>
        /// Clones a ScreenCoordinate
        /// </summary>
        /// <param name="coordinate">The IScreenCoordinate to clone</param>
        public ScreenCoordinate(IScreenCoordinate coordinate)
        {
            if (coordinate == null)
                throw new ArgumentNullException("coordinate",
                    Properties.Resources.ScreenCoordinateScreenCoordinateCoordinateNullArgumentException);

            _x = coordinate.X;
            _y = coordinate.Y;
        }

        #endregion Constructors

        #region Fields

        private double _x;
        private double _y;

        #endregion Fields

        #region Properties

        /// <summary>
        /// Gets the X value
        /// </summary>
        public double X { get { return _x; } }

        /// <summary>
        /// Gets the Y value
        /// </summary>
        public double Y { get { return _y; } }

        /// <summary>
        /// Gets the oordinate at the specified axis
        /// </summary>
        /// <param name="axis">The axis of the oordinate to retrieve</param>
        /// <returns>Returns the oordiante at the specified axis, if the axis not between 0 and 1, double.NaN is returned.</returns>
        public double this[int axis]
        {
            get
            {
                if (axis == 0)
                {
                    return _x; ;
                }
                else if (axis == 1)
                {
                    return _y;
                }
                else
                {
                    return double.NaN;
                }
            }
        }

        /// <summary>
        /// Gets the number of Axes (2)
        /// </summary>
        int ICoordinate.NumberOfAxes { get { return 2; } }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Tests for equality with the passed IScreenCoordinate
        /// </summary>
        /// <param name="obj">The IScreenCoordinate to test for equality with</param>
        /// <returns>Returns true if this is equal to the passed IScreenCoordinate</returns>
        public override bool Equals(object obj)
        {
            var other = obj as IScreenCoordinate;

            if (obj == null || other == null)
                return false;

            return other.X == X && other.Y == Y;
        }

        /// <summary>
        /// Tests for equality with the passed IScreenCoordinate
        /// </summary>
        /// <param name="other">The IScreenCoordinate to test for equality with</param>
        /// <returns>Returns true if this is equal to the passed IScreenCoordinate</returns>
        public bool Equals(IScreenCoordinate other)
        {
            if (other == null)
            {
                return false;
            }

            return other.X == X && other.Y == Y;
        }

        /// <summary>
        /// Calculates the distance between the passed IScreenCoordinate
        /// </summary>
        /// <param name="coordinate1">The first IScreenCoordinate</param>
        /// <param name="coordinate2">The second IScreenCoordinate</param>
        /// <returns>The distance between the two IScreenCoordinate</returns>
        public static double CalculateDistance(IScreenCoordinate coordinate1, IScreenCoordinate coordinate2)
        {
            if (coordinate1 == null)
                throw new ArgumentNullException("coordinate1",
                    Properties.Resources.ScreenCoordinateCalculateDistanceC1CoordinateNullArgumentException);

            if (coordinate2 == null)
                throw new ArgumentNullException("coordinate2",
                    Properties.Resources.ScreenCoordinateCalculateDistanceC2CoordinateNullArgumentException);

            double dx = coordinate2.X - coordinate1.X;
            double dy = coordinate2.Y - coordinate1.Y;

            return Math.Sqrt((dx * dx) + (dy * dy));
        }

        /// <summary>
        /// Calculates the distance between this ScreenCoordinate and the passed IScreenCoordinate
        /// </summary>
        /// <param name="other">The IScreenCoordinate to calculate the distance with</param>
        /// <returns>The linear distance between this and the passed IScreenCoordinate</returns>
        public double CalculateDistance(IScreenCoordinate other)
        {
            return CalculateDistance(this, other);
        }

        /// <summary>
        /// Converts this ScreenCoordinate to a string
        /// </summary>
        /// <returns>A string representing this ScreenCoordinate</returns>
        public override string ToString()
        {
            return string.Format(System.Globalization.CultureInfo.InvariantCulture, "({0},{1})", X, Y);
        }

        /// <summary>
        /// Calculates the hashcode of this ScreenCoordinate
        /// </summary>
        /// <returns>The computed hashcode of this IScreenCoordinate</returns>
        public override int GetHashCode()
        {
            unchecked
            {
                long l = BitConverter.DoubleToInt64Bits(_x);
                l *= l * 31 ^ BitConverter.DoubleToInt64Bits(_y);
                return (int)((l >> 32) ^ l);
            }
        }

        #endregion Methods
    }
}